//
//  AppManager.m
//  NhomTuan
//
//  Created by Buffalo on 1/28/15.
//  Copyright (c) 2015 Buffalo. All rights reserved.
//

#import "AppManager.h"

static AppManager *instance;
@implementation AppManager

+ (AppManager *)defaultmanager {
    if (!instance) {
        instance = [[AppManager alloc] init];
        [instance getEnSongList];
        [instance getViSongList];
        [instance getSingerList];
        [instance getFavorSongList];
        [instance getMembers];
    }
    return instance;
}

+ (void)save {
    [instance saveDateToPlist];
}

// Lay danh sach enSongList
- (void)getEnSongList {
    self.dataPath = [DocumentsDirectory stringByAppendingPathComponent:DATA_FILE];
    NSString *dataFileInbundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:DATA_FILE];
    
    //kiem tra su ton tai file data.plist trong thu muc Documents
    BOOL isPlistDataFileExits = [AppUtils checkFileOrDirExits:self.dataPath isDir:NO];
    if (!isPlistDataFileExits) {
        [AppUtils copyFile:dataFileInbundlePath toPath:self.dataPath];
    }
    self.enSongList = [AppUtils getArrayDataInPlist:self.dataPath withKey:KEY_EN_SONGS];
}

// Lay danh sach viSongList
- (void)getViSongList {
    self.viSongList = [AppUtils getArrayDataInPlist:self.dataPath withKey:KEY_VI_SONGS];
}

// Lay danh sach singersList
- (void)getSingerList {
    self.singerList = [AppUtils getArrayDataInPlist:self.dataPath withKey:KEY_SINGERS];
}
- (void)getFavorSongList {
    self.favorSongList = [NSMutableArray arrayWithArray:[AppUtils getArrayDataInPlist:self.dataPath withKey:KEY_FAVOR_SONG]];
}
- (void)getMembers {
    self.members = [AppUtils getArrayDataInPlist:self.dataPath withKey:KEY_MEMBERS];
}

- (void)saveDateToPlist {
    NSDictionary *plist = [[NSDictionary alloc] initWithObjectsAndKeys:self.enSongList, KEY_EN_SONGS, self.viSongList, KEY_VI_SONGS, self.singerList, KEY_SINGERS, self.favorSongList, KEY_FAVOR_SONG, self.members, KEY_MEMBERS, nil];
    [plist writeToFile:self.dataPath atomically:YES];
}
@end
